import 'dart:convert';

import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:ugrownews_flutter/utils/netutil/NetUtil.dart';
import 'package:ugrownews_flutter/utils/netutil/NewsNetUtil.dart';

class FragmentMainRequest extends StatefulWidget {
  @override
  _FragmentMainRequestState createState() {
    _FragmentMainRequestState _fragmentMainRequestState =
        _FragmentMainRequestState();
    _initData(_fragmentMainRequestState);
    return _fragmentMainRequestState;
  }

  void _initData(_FragmentMainRequestState _fragmentMainRequestState) {
    new NewsNetUtil().getRequestList(null, (result, isCache) {
      if (result is NetError) {
        return;
      }

      if (result is NetError) {
        return;
      } else {
        Map jsonResult = json.decode(result);
        String state = jsonResult["state"];
        if (state.isNotEmpty && state == "success") {
          if (_fragmentMainRequestState.mounted) {
            _fragmentMainRequestState.setState(() {
              List<dynamic> data = jsonResult["data"];
              _fragmentMainRequestState.data = data;
            });
          }
        }
      }
    });
  }
}

class _FragmentMainRequestState extends State<FragmentMainRequest>
    with AutomaticKeepAliveClientMixin {
  List<dynamic> data = new List();

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return new Container(
      child: new ListView.builder(
        padding: EdgeInsets.all(0),
        itemCount: data.length,
        itemBuilder: _buildItem,
      ),
    );
  }

  Widget _buildItem(BuildContext context, int index) {
    String imagepath = data[index]["listImage"];
    return new GestureDetector(
      child: new CachedNetworkImage(
        imageUrl: imagepath,
        placeholder: (context, url) {
          return new Image.asset(
            "images/image_loading1.png",
            height: ScreenUtil.getInstance().setWidth(610),
            width: ScreenUtil.screenWidthDp,
            fit: BoxFit.fill,
          );
        },
        errorWidget: (context, url, builder) {
          return new Icon(Icons.error);
        },
        height: ScreenUtil.getInstance().setWidth(610),
        width: ScreenUtil.screenWidthDp,
        fit: BoxFit.fill,
//      child: new Stack(
//        children: <Widget>[
//          new Image.asset(
//            "images/image_loading1.png",
//            height: ScreenUtil.getInstance().setWidth(608),
//            width: ScreenUtil.screenWidthDp,
//            fit: BoxFit.fill,
//          ),
//          new Image(
//            height: ScreenUtil.getInstance().setWidth(608),
//            width: ScreenUtil.screenWidthDp,
//            fit: BoxFit.fill,
//            image: AdvancedNetworkImage(
//              imagepath,
//              useDiskCache: false,
//              fallbackAssetImage: "images/image_loading1.png",
//              cacheRule: CacheRule(maxAge: const Duration(days: 7)),
//            ),
//          )
//        ],
      ),
    );
  }

  @override
  bool get wantKeepAlive => true;
}
